package com.ujf.GICOM_ldap;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;

public class ldap {

	public static void main(String[] args) throws NamingException {
		List<String> ips = ldap.getSondesForService("leBonCoin");
		
		for(String ip : ips){
			System.out.println("-"+ip);
		}
		
	}
	
	
	
	public static void getPI(){
		DirContext dirContext = LdapWrapper.getDirContext();
		try {
			System.out.println("Connection au serveur LDAP done");
			// Attributes attributs = dirContext
			// .getAttributes("cn=ECOM,ou=devices,dc=gicom,dc=org");
			// Attribute attribut = (Attribute) attributs.get("ipHostNumber") ;
			// System.out.println("Adressse IP de ECOM -> "+attribut.get());

			String recherche = "leBonCoin";
			System.out.println("Recherche sur le role " + recherche);

			Attributes matchattribs = new BasicAttributes(true);
			matchattribs.put(new BasicAttribute("cn", recherche));

			System.out.println("Device");
			NamingEnumeration resultat = dirContext.search(
					"ou=devices,dc=gicom,dc=org", matchattribs);
			while (resultat.hasMore()) {
				SearchResult sr = (SearchResult) resultat.next();
				System.out.println("IP : "
						+ sr.getAttributes().get("ipHostNumber").get());
			}

			System.out.println("Role :");
			resultat = dirContext.search("ou=rolesDevices,dc=gicom,dc=org",
					matchattribs);
			while (resultat.hasMore()) {
				SearchResult sr = (SearchResult) resultat.next();
				System.out.println("Role : "
						+ sr.getAttributes().get("cn").get());
			}

			System.out.println("Config :");
			resultat = dirContext.search("ou=rolesDevices,dc=gicom,dc=org",
					matchattribs);
			while (resultat.hasMore()) {
				SearchResult sr = (SearchResult) resultat.next();
				System.out.println("Role : "
						+ sr.getAttributes().get("cn").get());
			}

			// NamingEnumeration<? extends Attribute> it = attributs.getAll();
			// while(it.hasMoreElements()) {
			// System.out.println("-"+it.next());
			//
			// // System.out.println("Tel : " + ((Attribute)
			// attributs.get(it.next())).get());
			//
			// }

			System.out.println("Close");
			dirContext.close();
		} catch (NamingException e) {
			System.err.println("Erreur lors de l'acces au serveur LDAP" + e);
			e.printStackTrace();
		}
	}

	public static List<String> getSondesForService(String service) {

		List<String> ips = new ArrayList<String>();

		DirContext dirContext = LdapWrapper.getDirContext();

		// Connaitre l'adresse des sondes.
		System.out.println("Connaitre l'adresse des sondes de leBonCoin");

		try {

			System.out.println("Connection au serveur LDAP done");
			String recherche = "leBonCoin";
			Attributes matchattribs = new BasicAttributes(true);
			matchattribs.put(new BasicAttribute("cn", recherche));
			NamingEnumeration resultat = dirContext.search(
					"ou=rolesDevices,dc=gicom,dc=org", matchattribs);

			SearchResult sr = (SearchResult) resultat.next();
			Attribute attrs = sr.getAttributes().get("member");
			Object at = null;

			for (int i = 0; i < attrs.size(); i++) {

				at = attrs.get(i);

				String recherche2 = at.toString();
				Attributes matchattribs2 = new BasicAttributes(true);
				matchattribs.put(new BasicAttribute("cn", recherche2));
				NamingEnumeration<SearchResult> resultat2 = dirContext.search(
						"ou=devices,dc=gicom,dc=org", matchattribs2);

				SearchResult sr2 = resultat2.next();
				Attribute attrs2 = sr2.getAttributes().get("ipHostNumber");

				ips.add(attrs2.get().toString());
			}

		} catch (NamingException e) {
			System.err.println("Erreur lors de l'acces au serveur LDAP" + e);
			e.printStackTrace();
		}

		return ips;
	}
}
